package com.zidian.leader.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.zidian.leader.R;
import com.zidian.leader.entity.StudentStatisticsBean;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 学生行为adapter
 * Created by GongCheng on 2017/2/16.
 */

public class StudentStatisticsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_TOP = 0;
    private static final int TYPE_ITEM = 1;

    private List<StudentStatisticsBean> list;
    private Context context;

    public StudentStatisticsAdapter(Context context) {
        this.context = context;
    }

    public void setDate(List<StudentStatisticsBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_TOP) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_student_statistics_top, parent, false);
            return new TopViewHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_student_statistics, parent, false);
            return new ItemViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof TopViewHolder) {
            ((TopViewHolder) holder).collegeNameTv.setText(list.get(position).getItemName());
            if (list.get(position).getSum() != 0) {
                ((TopViewHolder) holder).attendanceRatePb.setVisibility(View.VISIBLE);
                ((TopViewHolder) holder).attendanceRatePb.setMax(list.get(position).getSum());
                ((TopViewHolder) holder).attendanceRatePb.setProgress(list.get(position).getNum());
                //格式化
                DecimalFormat format = new DecimalFormat("##0.0");
                int numSum = list.get(position).getSum();
                int num = list.get(position).getNum();
                float attendancePercentage = num / (numSum + 0.00f) * 100;
                ((TopViewHolder) holder).attendanceRateTv
                        .setText(context.getString(R.string.attendance_rate,
                                list.get(position).getNum(),
                                list.get(position).getSum(),
                                format.format(attendancePercentage)));
            } else {
                ((TopViewHolder) holder).attendanceRatePb.setVisibility(View.GONE);
                ((TopViewHolder) holder).attendanceRateTv.setText(R.string.attendance_none);
            }

        } else if (holder instanceof ItemViewHolder) {
            ((ItemViewHolder) holder).collegeNameTv.setText(list.get(position).getItemName());
            if (list.get(position).getSum() != 0) {
                ((ItemViewHolder) holder).attendanceRatePb.setVisibility(View.VISIBLE);
                ((ItemViewHolder) holder).attendanceRatePb.setMax(list.get(position).getSum());
                ((ItemViewHolder) holder).attendanceRatePb.setProgress(list.get(position).getNum());
                //格式化
                DecimalFormat format = new DecimalFormat("##0.0");
                int numSum = list.get(position).getSum();
                int num = list.get(position).getNum();
                float attendancePercentage = num / (numSum + 0.00f) * 100;
                ((ItemViewHolder) holder).attendanceRateTv
                        .setText(context.getString(R.string.attendance_rate,
                                list.get(position).getNum(),
                                list.get(position).getSum(),
                                format.format(attendancePercentage)));
            } else {
                ((ItemViewHolder) holder).attendanceRatePb.setVisibility(View.GONE);
                ((ItemViewHolder) holder).attendanceRateTv.setText(R.string.attendance_none);
            }



        }
    }

    @Override
    public int getItemCount() {

        return list == null ? 0 : list.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_TOP;
        } else {
            return TYPE_ITEM;
        }
    }

    public interface OnItemClickListener{
        void onItemClick(String college, int sum, int num);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.college_name_tv)
        TextView collegeNameTv;
        @Bind(R.id.attendance_rate_tv)
        TextView attendanceRateTv;
        @Bind(R.id.attendance_rate_pb)
        ProgressBar attendanceRatePb;

        ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(
                                list.get(getAdapterPosition()).getItemName(),
                                list.get(getAdapterPosition()).getSum(),
                                list.get(getAdapterPosition()).getNum());
                    }
                }
            });
        }
    }

    class TopViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.college_name_tv)
        TextView collegeNameTv;
        @Bind(R.id.attendance_rate_tv)
        TextView attendanceRateTv;
        @Bind(R.id.attendance_rate_pb)
        ProgressBar attendanceRatePb;

        TopViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
